package com.ruoyi.fantasy.common;

import com.huanzhidadi.fantasy.bean.domain.FantasyCity;
import com.huanzhidadi.fantasy.bean.domain.FantasyCityMap;
import com.huanzhidadi.fantasy.bean.domain.FantasyTitleType;
import com.ruoyi.fantasy.service.IFantasyCitiesService;
import com.ruoyi.fantasy.service.IFantasyCitymapService;
import com.ruoyi.fantasy.service.IFantasyTitleTypeService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @class_name: WorldInit
 * @package: com.ruoyi.fantasy.common
 * @Description: 初始化世界，一切一切的开始。
 * @creat_user: wang
 * @creat_date: 2018/8/27
 * @creat_time: 0:11
 **/
@Slf4j
@Component
public class WorldInit {

    @Autowired
    private IFantasyCitiesService fantasyCitiesService;

    @Autowired
    private IFantasyCitymapService fantasyCitymapService;

    @Autowired
	private SysUserMapper userMapper;

	@Autowired
	private IFantasyTitleTypeService fantasyTitleTypeService;

	//static修饰的是类变量，只在类加载时实例化一次。
    //城市map，以城市id为key，城市对象为value，由initCities()方法初始化。
    public static Map<String, FantasyCity> citiesMap = new HashMap<>();
	public static Map<String, FantasyCityMap> cityMapsMap = new HashMap<>();

	public static Map<String, SysUser> sysUserMap = new HashMap<>();
	// 称号类型
	public static Map<String, FantasyTitleType> titleTypeMap = new HashMap<>();

	/**
	 * @method: init
	 * @Description:  容器启动完成时执行该方法，进行初始化
	 * @Param: []
	 * @return: void
	 * @creat_user: wang
	 * @creat_date: 2018/8/27
	 * @creat_time: 0:14
	 * @modify1: by wang on 2018/8/27
	 **/
	@PostConstruct
	public void init() {
		long startTime = System.currentTimeMillis();

		log.info("世界初始化开始。");
		initCities();
		initCityMaps();
		initSysUsers();
		initTitleTypes();
		log.info("世界初始化结束。");
		log.info("初始化完成用时： {}秒", (System.currentTimeMillis() - startTime) / 1000f);
	}

	/**
	 * @method: initCities
	 * @Description:  城市初始化，把所有地点加载到内存map中。
	 * @Param: []
	 * @return: void
	 * @creat_user: wang
	 * @creat_date: 2018/8/27
	 * @creat_time: 0:15
	 * @modify1: by wang on 2018/8/27
	 **/
	public void initCities() {
		log.info("城市初始化开始。");
        List<FantasyCity> cities = fantasyCitiesService.selectFantasyCitiesList(null);
        for (FantasyCity city : cities) {
            citiesMap.put(city.getId(), city);
        }
		log.info("城市初始化结束。");
	}

    public void initCityMaps() {
        log.info("城市地点初始化开始。");

        List<FantasyCityMap> cityMapsList = fantasyCitymapService.selectFantasyCitymapList(null);
        log.info("城市大小：{}", cityMapsList.size());
        for (FantasyCityMap cityMap : cityMapsList) {
            cityMapsMap.put(cityMap.getId(), cityMap);
            //初始化时，将cityMap所属的city保存到redis中，供FantasyUserServiceImpl.moveUser更新用户所在城市使用，
            //以获取当前地点所属城市id，用于聊天功能区分用户所在城市。wanghui 2018.8.17
            //redisUtil.setValue(Constants.REDIS_CITYMAP_ + cityMap.getId(), cityMap.getCityid());
        }
        log.info("城市地点初始化结束。");
    }

    public void initSysUsers() {
		log.info("初始化系统用户开始。");
		SysUser sysUser = new SysUser();
		List<SysUser> sysUsers = userMapper.selectUserList(sysUser);
		for (SysUser user : sysUsers) {
			sysUserMap.put(user.getUserId().toString(), user);
		}

		log.info("初始化系统用户结束。");
	}

	private void initTitleTypes() {
		log.info("初始化称号类型开始。");
		List<FantasyTitleType> list = fantasyTitleTypeService.selectFantasyTitleTypeList(null);
		for (FantasyTitleType fantasyTitleType : list) {
			titleTypeMap.put(fantasyTitleType.getId(), fantasyTitleType);
		}

		log.info("初始化称号类型结束。");

	}
}
